{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-28T08:28:58.135952Z",
     "start_time": "2025-10-28T08:28:55.126725Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import time\n",
    "from typing import TypedDict\n",
    "from langchain_core.runnables import RunnableConfig\n",
    "from langgraph.constants import START, END\n",
    "from langgraph.graph import StateGraph\n",
    "from langgraph.types import CachePolicy\n",
    "from langgraph.cache.memory import InMemoryCache  # 是langgraph中的,⽽不是langchain中的。\n",
    "\n",
    "\n",
    "# 定义图的状态结构，包含数字和用户ID\n",
    "class State(TypedDict):\n",
    "    number: int\n",
    "    user_id: str\n",
    "\n",
    "\n",
    "# 定义配置信息结构，包含用户ID\n",
    "class ConfigSchema(TypedDict):\n",
    "    user_id: str\n",
    "\n",
    "\n",
    "def node_1(state: State, config: RunnableConfig):\n",
    "    \"\"\"\n",
    "    图节点函数，用于处理状态转换\n",
    "\n",
    "    Args:\n",
    "        state (State): 当前状态，包含number和user_id字段\n",
    "        config (RunnableConfig): 运行时配置，包含用户配置信息\n",
    "\n",
    "    Returns:\n",
    "        dict: 更新后的状态，number加1，user_id从配置中获取\n",
    "    \"\"\"\n",
    "    time.sleep(3)\n",
    "    user_id = config[\"configurable\"][\"user_id\"]\n",
    "    return {\"number\": state[\"number\"] + 1, \"user_id\": user_id}\n",
    "\n",
    "\n",
    "# 构建状态图\n",
    "builder = StateGraph(\n",
    "    State, \n",
    "    context_schema=ConfigSchema\n",
    ")\n",
    "# 添加节点，设置5秒缓存策略\n",
    "builder.add_node(\"node1\", node_1, cache_policy=CachePolicy(ttl=5))\n",
    "\n",
    "# 添加起始边和结束边\n",
    "builder.add_edge(START, \"node1\")\n",
    "builder.add_edge(\"node1\", END)\n",
    "# 编译图并设置内存缓存\n",
    "graph = builder.compile(cache=InMemoryCache())\n",
    "# graph = builder.compile()\n",
    "\n",
    "# 第一次调用，执行节点逻辑\n",
    "print(graph.invoke({\"number\": 5}, config={\"configurable\": {\"user_id\": \"123\"}}, stream_mode='updates'))\n",
    "# [{'node1': {'number': 6, 'user_id': '123'}}]\n",
    "\n",
    "# 第二次调用，由于入参相同触发缓存机制\n",
    "print(graph.invoke({\"number\": 5}, config={\"configurable\": {\"user_id\": \"456\"}}, stream_mode='updates'))\n",
    "# [{'node1': {'number': 6, 'user_id': '123'}, '__metadata__': {'cached': True}}]\n"
   ],
   "id": "756add07ad7cd674",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'node1': {'number': 6, 'user_id': '123'}}]\n",
      "[{'node1': {'number': 6, 'user_id': '123'}, '__metadata__': {'cached': True}}]\n"
     ]
    }
   ],
   "execution_count": 6
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
